﻿using In66.Net.EventBus.RabbitMq.IIntegration;
using Microsoft.Extensions.DependencyInjection;

namespace In66.Net.EventBus.RabbitMq.Manager;

/// <summary>
/// 后台任务进行事件订阅
/// </summary>
internal class SubscribeService : BackgroundService
{
    private IServiceProvider _serviceProvider { get; set; }
    public SubscribeService(IServiceProvider serviceProvider)
    {
        _serviceProvider = serviceProvider;

    }
    protected override async Task ExecuteAsync(CancellationToken cancelToken)
    {
        using var scope = _serviceProvider.CreateScope();
        var eventBus = scope.ServiceProvider.GetService<IIntegrationEventBus>() as IntegrationEventBus ?? throw new("RabbitMQ集成事件总线没有注册");
        eventBus.Subscribe();
        while (!cancelToken.IsCancellationRequested)
        {
            await Task.Delay(5000, cancelToken);
        }
    }
}